package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/05/02/下午 5:50;
 * Description
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;

    @Override
    public void submit(Orders orders) {
        //获取当前用户信息
        Long currentId = BaseContext.getCurrentId();//用户id
        //获取当前user的信息
        User user = userService.getById(currentId);//获取用户对象
        //
        BigDecimal total = new BigDecimal(0);//订单总金额

        //
        String address = orders.getAddress();//地址id
        AddressBook addressBook = addressBookService.getById(address);//获取地址对象

        //查询购物车
        QueryWrapper<ShoppingCart> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("used_id", currentId);

        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper1);

        /*if (shoppingCarts==null&&shoppingCarts.size()==0){
            throw new CustomException("购物车为空，不能下单");
        }*/
        //orders.setId();
        long ordersId = IdWorker.getId();//订单的id


        //2 订单详情n条
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {

            OrderDetail orderDetail = new OrderDetail();

            orderDetail.setNumber(cart.getNumber());
            orderDetail.setName(cart.getName());
            orderDetail.setId(ordersId);
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            // 乘.multiply
            orderDetail.setAmount(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));//小计
            total.add(orderDetail.getAmount());//了累计小计得到总金额
            orderDetail.setImage(cart.getImage());
            orderDetails.add(orderDetail);
        }

        //1.
        orders.setId(ordersId);
        orders.setNumber("0_" + System.currentTimeMillis());
        orders.setStatus(2);
        orders.setUserId(user.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(total);
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());

        //3.保存数据
        this.save(orders);
        //
        orderDetailService.saveBatch(orderDetails);
        //4.清空购物车
        shoppingCartService.remove(queryWrapper1);
    }
}
